package com.nari.ncps.screen.service.impl.dryport;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.dryport.DryportProdoperTransshipment;
import com.nari.ncps.screen.mapper.dryport.DryportProdoperTransshipmentMapper;
import com.nari.ncps.screen.service.dryport.DryportProdoperTransshipmentService;
import com.nari.ncps.screen.vo.dryport.TransshipmentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 倒运量情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class DryportProdoperTransshipmentServiceImpl extends ServiceImpl<DryportProdoperTransshipmentMapper, DryportProdoperTransshipment> implements DryportProdoperTransshipmentService {

    @Autowired
    DryportProdoperTransshipmentMapper dryportProdoperTransshipmentMapper;

    @Override
    public List<TransshipmentVO> getMonthlyStatsByYear(Integer year) {
        // 从数据库查询数据
        List<DryportProdoperTransshipment> entityList = baseMapper.getMonthlyStatsByYear(year);
        
        // 转换为VO对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return entityList.stream().map(entity -> {
            TransshipmentVO vo = new TransshipmentVO();
            BeanUtils.copyProperties(entity, vo);
            // 将Date类型转换为String类型
            if (entity.getDate() != null) {
                vo.setDate(sdf.format(entity.getDate()));
            }
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<TransshipmentVO> getLatest30DaysStats() {

        // 生成最近30天的日期列表
        List<String> last30Days = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        for (int i = 29; i >= 0; i--) {
            LocalDate date = currentDate.minusDays(i);
            last30Days.add(date.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
        }

        // 构建查询条件
        LambdaQueryWrapper<DryportProdoperTransshipment> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DryportProdoperTransshipment::getOrgCode, "100360")
                .in(DryportProdoperTransshipment::getStatisticTime, last30Days)
                .orderByAsc(DryportProdoperTransshipment::getStatisticTime);

        // 执行查询
        List<DryportProdoperTransshipment> entityList = dryportProdoperTransshipmentMapper.selectList(lqw);

        // 创建日期到实体的映射
        Map<String, DryportProdoperTransshipment> entityMap = entityList.stream()
                .collect(Collectors.toMap(DryportProdoperTransshipment::getStatisticTime, Function.identity()));

        // 构建完整的30天数据，缺失的日期用空数据填充
        List<TransshipmentVO> voList = new ArrayList<>();
        for (String dateStr : last30Days) {
            DryportProdoperTransshipment entity = entityMap.get(dateStr);
            TransshipmentVO vo = new TransshipmentVO();

            if (entity != null) {
                // 有数据的日期
                vo.setYear(entity.getYear());
                vo.setMonth(entity.getMonth());
                vo.setDay(entity.getDay());
                vo.setTransshipmentPlanned(entity.getTransshipmentPlanned());
                vo.setTransshipmentActual(entity.getTransshipmentActual());
                vo.setCompletionRate(entity.getCompletionRate());
            } else {
                // 没有数据的日期，设置默认值
                LocalDate date = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyyMMdd"));
                vo.setYear(date.getYear());
                vo.setMonth(date.getMonthValue());
                vo.setDay(date.getDayOfMonth());
                vo.setTransshipmentPlanned(BigDecimal.ZERO);
                vo.setTransshipmentActual(BigDecimal.ZERO);
                vo.setCompletionRate(BigDecimal.ZERO);
            }

            // 设置日期字符串
            vo.setDate(dateStr.substring(0, 4) + "-" +
                    dateStr.substring(4, 6) + "-" +
                    dateStr.substring(6, 8));
            voList.add(vo);
        }

        return voList;
    }

}
